import React, { Component } from 'react'
// eslint-disable-next-line
import { NavLink, Route, BrowserRouter, HashRouter, Switch, Redirect } from 'react-router-dom'
import Home from './Home'
import About from './About'
// 普通组件和路由组件最大的区别：路由组件收到三个固定属性(history, location, match)
import Header from './Header'
// 封装NavLink
import MyNavLink from './MyNavLink'

export class HashRouterDemo extends Component {

  push = (path) => {
    window.location.hash = `#${path}`
  }

  back = () => {
    window.history.back()
  }

  forward = () => {
    window.history.forward()
  }

  render() {
    return (
      <div>
        <button onClick={() => this.push('/test1')}>push test1</button><br /><br />
        <button onClick={() => this.back()}>&lt;= 回退</button>
        <button onClick={() => this.forward()}>前进 =&gt;</button>
      </div>
    )
  }
}

export class HistoryRouterDemo extends Component {

  push = (path) => {
    const state = { name: path }
    window.history.pushState(state, '', path)
  }

  replace = (path) => {
    const state = { name: path }
    window.history.replaceState(state, '', path)
  }

  back = () => {
    window.history.back()
  }

  forward = () => {
    window.history.forward()
  }

  render() {
    return (
      <div>
        <a href="http://www.atguigu.com" onClick={() => { this.push('/test1') }}>push test1</a><br /><br />
        <button onClick={() => this.push('/test2')}>push test2</button><br /><br />
        <button onClick={() => this.replace('/test3')}>replace test3</button><br /><br />
        <button onClick={() => this.back()}>&lt;= 回退</button>
        <button onClick={() => this.forward()}>前进 =&gt;</button>
      </div>
    )
  }
}

export class ReactRouterDemo extends Component {
  render() {
    return (
      <BrowserRouter>
        <div>
          <div className="row">
            <div className="col-xs-offset-2 col-xs-8">
              <Header />
            </div>
          </div>
          <div className="row">
            <div className="col-xs-2 col-xs-offset-2">
              <div className="list-group">
                {/* 在React中靠路由链接实现切换组件--编写路由链接 */}
                {/* NavLink 会追加 active 属性 */}
                <MyNavLink to="/about">About</MyNavLink>
                <MyNavLink to="/home">Home</MyNavLink>
              </div>
            </div>
            <div className="col-xs-6">
              <div className="panel">
                <div className="panel-body">
                  {/* 注册路由 */}
                  {/* 注意：如果多个path同时匹配上, 会同时展示, 需要使用 Switch 解决无限次匹配路由的问题！！ */}
                  {/* 路由匹配默认是模糊匹配 to="/home/a/b" 会匹配上 path="/home" 的组件 */}
                  {/* 使用 exact 开启精准匹配, 严格匹配要慎用！有时候开启会导致无法继续匹配二级路由！！ */}
                  <Switch>
                    <Route exact path="/about" component={About} />
                    {/* 这里home后面有二级路由, 所以这里不能开启严格模式！！ */}
                    <Route path="/home" component={Home} />
                    {/* 当上面的都匹配不上的时候, 走 Redirect */}
                    <Redirect to="/about"></Redirect>
                  </Switch>
                </div>
              </div>
            </div>
          </div>
        </div>
      </BrowserRouter>
    )
  }
}
